﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VWise_DAO;

namespace VWISE_MantVehicles
{
  public partial class CreateForm : Form
  {
    CVWise_DAO_Access pVWise_DAO_Access = new CVWise_DAO_Access();
    List<CConductor> lListConductores = null;

    public CreateForm()
    {
      InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
      //internal - configuracion de la consulta asincrona
      CLogProcessor.InitLog();
      pVWise_DAO_Access.Configure();

      this.Show();
      botonCrear.Enabled = false;
        
      FillComboBox();

      botonCrear.Enabled = true;

      labelStatus.Visible = false;
        
    }

    void FillComboBox()
    {
      lListConductores = pVWise_DAO_Access.GetAllConductores();

      if (lListConductores == null)
      {
        botonCrear.Enabled = false;
        return;
      }

      if (lListConductores.Count == 0)
      {
        botonCrear.Enabled = false;
        return;
      }


      foreach (CConductor pConductor in lListConductores)
      {
        comboConductor.Items.Add(pConductor.szConductorDesc);
      }
    }

    private void botonCancelar_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    void BuscarVehicle()
    {
      List<CVehicle> lListVehicle = null;
      int iIndex = -1;
      
      lListVehicle = pVWise_DAO_Access.SearchForVehicle("","","",textPlaca.Text,"","");

      if (lListVehicle == null)
      {
        MessageBox.Show(this, "No se encontraron resultados", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
      }

      if (lListVehicle.Count == 0)
      {
        MessageBox.Show(this, "No se encontraron resultados", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return;
      }

      //placa debe ser unica

      foreach (CVehicle pVehicle in lListVehicle)
      {
        textCodigo.Text = pVehicle.iPKCode.ToString("000000000");
        textPlaca.Text = pVehicle.szPlaca;
        textMarca.Text = pVehicle.szMarca;
        textTipo.Text = pVehicle.szTipoVehiculo;
        textModelo.Text = pVehicle.szModelo;
        textPropietario.Text = pVehicle.szPropietario;

        iIndex = 0;
        foreach (CConductor pConductor in lListConductores)
        {
          if (pConductor.iPKCode == pVehicle.iPKCodeConductor)
          {
            comboConductor.SelectedIndex = iIndex;
            break;
          }
          iIndex++;
        }
        break;
      }
    }

    bool ValidateData()
    {
      if (textPlaca.Text == "")
        return false;

      if (textMarca.Text == "")
        return false;

      if (textModelo.Text == "")
        return false;

      if (textPropietario.Text == "")
        return false;

      if (comboConductor.SelectedIndex == -1)
        return false;

      return true;
    }

    private void botonCrear_Click(object sender, EventArgs e)
    {
      List<CVehicle> lListVehicle = null;
      CVehicle pVehicle = null;
      int iresult;

      //validacion de datos
      if (!ValidateData())
      {
        MessageBox.Show(this, "Por favor revise los datos a registrar", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }

      //busquemos si existe:
      lListVehicle = pVWise_DAO_Access.SearchForVehicle("", "", "", textPlaca.Text, "", "");

      if (lListVehicle != null)
      {
        if(lListVehicle.Count > 0)
        {
          MessageBox.Show(this, "La placa " + textPlaca.Text + " ya se encuentra registrada.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
          return;
        }
      }

      //no existe entonces registramos el vehiculo
      pVehicle = new CVehicle();

      pVehicle.iPKCode = 0;
      pVehicle.szPlaca = textPlaca.Text;
      pVehicle.szMarca = textMarca.Text;
      pVehicle.szModelo = textModelo.Text;
      pVehicle.szTipoVehiculo = textTipo.Text;
      pVehicle.szPropietario = textPropietario.Text;
      pVehicle.iPKCodeConductor = lListConductores[comboConductor.SelectedIndex].iPKCode;


      iresult = pVWise_DAO_Access.CreateNewVehicle(pVehicle);
      if (iresult != 0)
      {
        //error
        MessageBox.Show(this, "La placa " + textPlaca.Text + " no pudo ser registrada.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }

      MessageBox.Show(this, "La placa " + textPlaca.Text + " se registro correctamente.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
      return;


    }
  }
}
